package com.wuhongyu.controller;

import com.wuhongyu.entity.Record;
import com.wuhongyu.service.CustomerService;
import com.wuhongyu.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 客户的交往记录
 */
@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/list")
    public String list(Model model,Integer customerid){
        model.addAttribute("recordList",recordService.findAll(customerid));
        model.addAttribute("customer",customerService.findOne(customerid));
        return "~cust/cust/activities";
    }

    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id,Integer customerid) {
        if(id!=null && customerid==null){
            model.addAttribute("record",recordService.findOne(id));
            return "~cust/cust/activities_edit";
        }
        model.addAttribute("customer",customerService.findOne(customerid));
        return "~cust/cust/activities_add";
    }
    @RequestMapping("/save")
    public String save(Model model, Record record) {
        recordService.save(record);
        return this.list(model,record.getCustomer().getId());
    }
    @RequestMapping("/delete")
    public String delete(Model model, Integer id) {
        Record record = recordService.findOne(id);
        recordService.delete(id);
        return this.list(model,record.getCustomer().getId());
    }
}